package com.jly.controller;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.jly.common.Result;
import com.jly.entity.Book;
import com.jly.entity.Order;
import com.jly.entity.User;
import com.jly.mapper.BookMapper;
import com.jly.mapper.OrderMapper;
import com.jly.utils.TokenUtils;
import org.bouncycastle.pqc.math.linearalgebra.IntUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    OrderMapper orderMapper;

    @Resource
    BookMapper bookMapper;

    @PostMapping
    public Result save(@RequestBody Order Order) {
        orderMapper.insert(Order);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody Order Order) {
        orderMapper.updateById(Order);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        orderMapper.deleteById(id);
        return Result.success();
    }
    @PostMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        if (ids != null && !ids.isEmpty()) {
            orderMapper.deleteBatchIds(ids);
            return Result.success();
        }
        return Result.error();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id) {
        return Result.success(orderMapper.selectById(id));
    }

    @GetMapping("/buy/{bookId}")
    public Result buy(@PathVariable Long bookId) {
        Book book = bookMapper.selectById(bookId);
        String orderNo = IdUtil.getSnowflake().nextIdStr();
        String payUrl = "http://localhost:9090/alipay/pay?subject=" + book.getName() + "&traceNo=" + orderNo + "&totalAmount=" + book.getPrice();

        //User user = getUser();
        Order order = new Order();
        order.setOrderNo(orderNo);
        order.setTotalPrice(book.getPrice());
        order.setPayPrice(book.getPrice());
        order.setTransportPrice(BigDecimal.ZERO);
        order.setUserId(Objects.requireNonNull(TokenUtils.getCurrentUser()).getId());
        order.setUsername(TokenUtils.getCurrentUser().getUsername());
//        order.setUserId(user.getId());
//        order.setUsername(user.getUsername());
        order.setName(book.getName());
        save(order);
        // 新建订单，扣减库存
        return Result.success(payUrl);
    }

    @GetMapping
    public Result findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String name) {
        LambdaQueryWrapper<Order> wrapper = Wrappers.lambdaQuery();
        if (StrUtil.isNotBlank(name)) {
            wrapper.like(Order::getOrderNo, name);
        }
        Page<Order> OrderPage = orderMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(OrderPage);
    }
}
